package com.tunestore.action;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;

public class DownloadAction extends Action {
  private static final Log log = LogFactory.getLog(DownloadAction.class);

  public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    DynaActionForm daf = (DynaActionForm)form;
    
    try {
      // Try to open the stream first - if there's a goof, it'll be here
      InputStream is = this.getServlet().getServletContext().getResourceAsStream("/WEB-INF/bits/" + request.getParameter("cd"));
      
      Statement stmt = conn.createStatement();
      ResultSet rs0 = null;
      String albumID = "";
      if(daf.getString("cd").equalsIgnoreCase("anka.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Paul Anka'");
      rs0.next();
      albumID = rs0.getString(1);
      }
      if(daf.getString("cd").equals("bennett.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Tony Bennett'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("chumbawamba.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Chumbawamba'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("cuomo.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Perry Cuomo'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("khan.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Chaka Khan'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("midler.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Better Midler'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("manilow.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Barry Manilow'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("newton.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Wayne Newton'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("sade.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Sade'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      if(daf.getString("cd").equals("sinatra.mp3"))
      {
      rs0 = stmt.executeQuery("SELECT ID FROM CD WHERE ARTIST='Frank Sinatra'");
      rs0.next();
	  albumID = rs0.getString(1);
  }
      PreparedStatement prep = conn.prepareStatement("SELECT * FROM TUNEUSER_CD WHERE TUNEUSER=?");
      prep.setString(1, request.getSession(true).getAttribute("USERNAME").toString());
      ResultSet rs = prep.executeQuery();
      boolean hasPermission = false;
      while(rs.next())
      {
    	  String id = rs.getString(2);
    	  if(id.equals(albumID))
    	  {
    		  hasPermission = true;
    	  }
      } 
      if (is != null && hasPermission) {
        response.setContentType("audio/mpeg");
        response.setHeader("Content-disposition", "attachment; filename=" + daf.getString("cd"));    
        byte[] buff = new byte[4096];
        int bread = 0;
        while ((bread = is.read(buff)) >= 0) {
          response.getOutputStream().write(buff, 0, bread);
        }
      } else {
        ActionMessages errors = getErrors(request);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("download.error"));
        saveErrors(request, errors);
        return mapping.findForward("error");        
      }
    } catch (Exception e) {
      e.printStackTrace();
      ActionMessages errors = getErrors(request);
      errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("download.error"));
      saveErrors(request, errors);
      return mapping.findForward("error");
    }
    
    return null;
  }
}
